<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>消息</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="list"
        style="width: 100%"
              v-loading="listLoading"
              element-loading-text="Loading"
              border
              fit
              highlight-current-row>
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="proName" label="所属项目名称" width="120">
        <template slot-scope="scope">
          <el-button type="text"
                     content="查看详情"
                     @click="projectDetail(scope.row.id)"
          >{{scope.row.proName}}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="taskName" label="任务名称" width="150"></el-table-column>
      <el-table-column prop="taskType" label="任务类型" width="120"></el-table-column>
      <el-table-column prop="demand" label="任务需求" width="250"></el-table-column>
      <el-table-column prop="distributeName" label="指派人"></el-table-column>
      <el-table-column prop="beDistributeName" label="接收人"></el-table-column>
      <el-table-column prop="createDate" label="时间" width="140"></el-table-column>
      <el-table-column label="操作" width="120" align="center" >
        <template slot-scope="scope">
          <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="openDeleteConfirm(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>


    <el-pagination
        align="center"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="limits"
        layout="prev, pager, next, jumper"
        :total="count">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      timer:null,
      checkList: ['选中且禁用','复选框 A'],
      count: 0,
      currentPage: 1,
      limits: 8,
      list: [],
      listLoading: true
    }
  },
  methods: {
    /* 查询知识点总数 */
    countNew() {
      console.log('newCount');
      let url = 'http://localhost:8989/news/newCount';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.count = responseBody.data;
      })
    },
    /* 分页查询 */
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      console.log("条数:" + this.limits);
      let limit = this.limits;
      let start = (val - 1)* this.limits;
      console.log("条数:" + limit);
      let url = 'http://localhost:8989/news/' + start + '/' + limit + '/newListPage';
      console.log('url = ' + url);
      this.listLoading = true;
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.list = responseBody.data;
        this.listLoading = false;
      })
    },
    projectDetail(id) {
      this.$router.push({path: '/projects/detail', query: {id}})
    },
    projectResultDetails(id){
      this.$router.push({path: '/projects/detail', query: {id}})
    },
    /* 删除 */
    handleDelete(New) {
      let url = 'http://localhost:8989/news/' + New.id + '/delete';
      console.log('url = ' + url);
      this.axios.create({'headers': {'Authorization': localStorage.getItem('jwt')}})
      .post(url).then((response) => {
        let responseBody = response.data;
        console.log(responseBody);
        if (responseBody.state != 20000) {
          this.$message.error(responseBody.message);
        }
        if (responseBody.state == 20000 || responseBody.state == 40400) {
          this.loadNewList();
        }
      })
    },
    openDeleteConfirm(New) {
      const message = '此操作将删除【' + New.taskName + '】消息，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(New);
      }).catch(() => {
      });
    },
    /* 首次访问查询知识点列表 */
    loadNewList() {
      console.log('loadProjectList');
      /*第一页显示8条数据*/
      let url = 'http://localhost:8989/news/0/8/newListPage';
      console.log('url = ' + url);
      this.listLoading = true;
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.list = responseBody.data;
        this.listLoading = false;
      })
    }
  },
  mounted() {
    console.log('初始化项目列表数据');
    this.loadNewList();
    this.countNew();
  }
}

</script>
